﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace DesignMyStack
{
    class Program
    {
        static void Main(string[] args)
        {
            MyStack<char> sta = new MyStack<char>();
            Console.WriteLine("演示Push()方法:");
            sta.Push('A');
            sta.Push('B');
            sta.Push('C');
            sta.Push('D');
            sta.Push('E');
            sta.Push('F');
            Console.WriteLine("演示Foreach()方法:");
            foreach (char c in sta)
            {
                Console.WriteLine(c);
            }
            Console.WriteLine("演示Pop()方法:");
            sta.Pop();
            sta.Pop();
            Console.WriteLine("演示Peek()方法:");
            sta.Peek();
            Console.WriteLine("演示Count的用法:");
            Console.WriteLine("MyStack have {0} character", sta.Count);
            Console.WriteLine("演示重写过的ToSring():");
            Console.WriteLine(sta.ToString());
            Console.WriteLine("演示Empty的用法:");
            sta.Empty();
            Console.WriteLine("After Empty() MyStack have {0} character", sta.Count);
            Console.WriteLine("调用过Empty()后使用Pop()的结果：");
            sta.Pop();
            Console.WriteLine("调用过Empty()后使用Peek()的结果：");
            sta.Peek();
            Console.ReadKey();
        }
        #region  The  generic interface IStack<T>
        interface IStack<T>
        {
            void Push(T item);
            void Pop();
            void Peek();
            void Empty();
            int Count { get; }
        }
        #endregion
        // Design MyStack<T>
        public class MyStack<T> : IStack<T>, IEnumerable, IEnumerator
        {
            int pi = -1;
            private int index;
            List<T> myList;
            public MyStack()
            {
                index = -1;
                myList = new List<T>();
            }
            #region 实现List<T>接口
            //Count
            public int Count
            {
                get { return myList.Count; }
            }
            //Push()
            public void Push(T item)
            {
                myList.Add(item);
                index++;
                Console.WriteLine("Push {0} into MyStack:", myList[index]);
            }
            //Pop()
            public void Pop()
            {
                try
                {
                    Console.WriteLine("Pop {0} out!", myList[index]);
                    myList.RemoveAt(index);
                    index--;
                }
                catch (Exception) { Console.WriteLine("MyStack is  Empty!"); }
            }
            //Peek()
            public void Peek()
            {
                try { Console.WriteLine(myList[index]); }
                catch (Exception) { Console.WriteLine("MyStack is Empty!"); }
            }
            //Empty
            public void Empty()
            {
                Console.WriteLine("Call Empty() ");
                myList.Clear();
                index = -1;
            }
            #endregion
            #region 支持foreach
            public IEnumerator GetEnumerator()
            {
                return this;
            }
            public object Current
            {

                get { return myList[pi]; }
            }
            public bool MoveNext()
            {
                if (pi >= myList.Count - 1)
                {
                    this.Reset();
                    return false;
                }
                pi++;
                return true;
            }
            public void Reset()
            {
                pi = -1;
            }
            #endregion
            #region 重写ToString()
            public override string ToString()
            {
                string str = Environment.NewLine;
                string temp = "";
                foreach (object t in myList)
                {
                    temp = t + str + temp;
                }
                return temp;
            }


            #endregion
        }
    }
}